from PyQt5.QtWidgets import QMainWindow, QTabWidget, QMessageBox, QLabel
from gui.config_panel import ConfigPanel
from gui.entry_list import EntryList
from gui.theme_panel import ThemePreviewPanel
from gui.kernel_param_editor import KernelParamEditor
from gui.animation_preview import RotatingMenuPreview
from core.manager import GrubConfigManager

class GrubEditorWindow(QMainWindow):
    def __init__(self, parser, offline_mode=False):
        super().__init__()
        self.setWindowTitle("GRUB2图形化编辑器")
        self.resize(1000, 600)
        self.parser = parser
        self.manager = GrubConfigManager(offline_mode=offline_mode)
        self.offline_mode = offline_mode

        self.tabs = QTabWidget()
        self.setCentralWidget(self.tabs)

        self.status_bar = self.statusBar()

        # 添加标签页
        self.config_panel = ConfigPanel(self.parser.get_global_config(), offline_mode=offline_mode)
        self.config_panel.apply_requested.connect(self.handle_config_update)
        self.tabs.addTab(self.config_panel, "通用设置")
        self.tabs.addTab(EntryList(), "启动项管理")
        self.tabs.addTab(ThemePreviewPanel(), "主题设置")
        self.tabs.addTab(KernelParamEditor(""), "内核参数")
        self.tabs.addTab(RotatingMenuPreview(), "动画预览")

        self.status_bar.showMessage("就绪")

        if offline_mode:
            info_label = QLabel("⚠️ 当前为模拟模式，无法执行实际 GRUB 配置更新。")
            info_label.setStyleSheet("color: red; font-weight: bold;")
            self.statusBar().addWidget(info_label, 1)

    def handle_config_update(self, new_config):
        result = self.manager.update_config(new_config)
        if result:
            self.status_bar.showMessage("配置已更新！")
            QMessageBox.information(self, "成功", "GRUB配置已成功更新！")
        else:
            self.status_bar.showMessage("配置更新失败")
            if self.offline_mode:
                QMessageBox.information(self, "模拟成功", "配置已保存为本地文件。")
            else:
                QMessageBox.critical(self, "错误", "配置更新失败，请查看日志")